package com.shujia;

import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class ReadCookie {
    public static void main(String[] args) throws IOException, ParseException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("E:\\workspace\\ideaProjects\\bigdata27-mypro\\redis-jedis\\src\\main\\resources\\cookie.txt"));
        String cookieString;
        String transform = "E MMM dd HH:mm:ss z yyyy";
        Set<Cookie> cookies = new HashSet<>();
        HashMap<String, String> cookie = new HashMap<>();
        while ((cookieString = bufferedReader.readLine()) != null) {
            String[] split = cookieString.split("=");
            cookie.put(split[0], split[1]);
            if ("value".equals(split[0])) {
                cookies.add(new Cookie(cookie.get("name")
                        , cookie.get("value")
                        , cookie.get("domain")
                        , cookie.get("path")
                        , new SimpleDateFormat(transform,Locale.US).parse(cookie.get("expiry"))
                        , Boolean.getBoolean(cookie.get("isSecure"))
                        , Boolean.getBoolean(cookie.get("isHttpOnly"))
                        , cookie.get("sameSite")
                ));
            }
        }
        login(cookies);
    }
    private static void login(Set<Cookie> cookies){
        System.setProperty("webdriver.edge.driver","E:\\workspace\\ideaProjects\\bigdata27-mypro\\redis-jedis\\src\\main\\resources\\msedgedriver.exe");
        System.out.println(System.getProperty("webdriver.edge.driver"));
        WebDriver edgeDriver = new EdgeDriver();
        edgeDriver.get("https://www.jd.com");
        edgeDriver.manage().deleteAllCookies();
       cookies.forEach(cookie ->edgeDriver.manage().addCookie(cookie));
       edgeDriver.navigate().refresh();
    }
}
